package v2.parser

/**
 * @author bweber
 * @version $Rev$, $Date$
 */

class Bunch private (val typeref:TypeRef,var slots:List[Slot]) extends Value with Slotted with Linkable {

  override def toString() = {
    "bunch of type " + typeref + " with slots " + slots
  }
  
  def getType() = {
    Type(typeref)
  }
  
  def setForAll(attrName:String,value:Value):Bunch = {
    for ( slot <- slots ) {
      val valu = slot.value
      if ( valu.isInstanceOf[Single]) {
        val single = valu.asInstanceOf[Single]
        single.setAttr(attrName,value)
      }
      else if ( valu.isInstanceOf[Bunch]) {
        val bunch = valu.asInstanceOf[Bunch]
        for ( bslot <- bunch.slots ) {
          if ( bslot.attrref == attrName ) {
            bslot.value = value
          }
        }
      }
      else {
        println("ignoring " + attrName + " on bunch for now...")
      }
    }
    this
  }
}

object Bunch {
  
  def apply(typeref:TypeRef,slots:Slot*) = {
    new Bunch(typeref,slots.toList)
  }
  
  def apply(typeref:TypeRef,slots:List[Slot]) = {
    new Bunch(typeref,slots)
  }
}